# -*- coding: utf-8 -*-
from datetime import timedelta
from jms.dwd.oms.dwd_yl_oms_order_mark_expand_base_dt import jms_dwd__dwd_yl_oms_order_mark_expand_base_dt
from time import time
from utils.operators.rich_sql_sensor import RichSqlSensor

label = f"dwd_yl_oms_order_mark_expand_base_dt_{int(time())}"
timeout = timedelta(minutes=10).seconds
doris_jms_dwd__dwd_yl_oms_order_mark_expand_base_dt = RichSqlSensor(
    task_id='doris_jms_dwd__dwd_yl_oms_order_mark_expand_base_dt',
    email=['rabie.zhuang@jtexpress.com', 'yl_bigdata@yl-scm.com'],
    pool='broker_load_pool',
    conn_id='doris',
    pre_sql=f"""
             TRUNCATE TABLE jms_dwd.dwd_yl_oms_order_mark_expand_base_dt PARTITION (p{{{{ execution_date | cst_ds_nodash }}}});
             LOAD LABEL jms_dwd.{label} (
                 DATA INFILE("hdfs://{{{{var.value.hadoop_namespace}}}}/dw/hive/jms_dwd.db/external/dwd_yl_oms_order_mark_expand_base_dt/dt={{{{ execution_date | cst_ds }}}}/*") 
                 INTO TABLE dwd_yl_oms_order_mark_expand_base_dt
                    FORMAT AS 'PARQUET'(
                    id,order_id,waybill_id,order_source_code,field_name,field_value,input_time,update_time,is_delete
                    )
                    columns from path as (dt)
                    set (
                    id=id,order_id=order_id,waybill_id=waybill_id,order_source_code=order_source_code,field_name=field_name,field_value=field_value,input_time=input_time,update_time=update_time,is_delete=is_delete,input_date=dt
                    )
                    ) 
             WITH BROKER '{{{{ var.json.doris_brokers | random_choice }}}}'
             PROPERTIES ('timeout'='{timeout}', 'max_filter_ratio'='0.0')""",
    poke_sql=f"SHOW LOAD FROM jms_dwd WHERE label = '{label}' ORDER BY CreateTime DESC LIMIT 1",
    sql_on_kill=f"CANCEL LOAD FROM jms_dwd WHERE LABEL = '{label}'",
    success=lambda r: r[2] == 'FINISHED',
    failure=lambda r: (r[2] is not None and r[2] == 'CANCELLED', str(r[7])),
    poke_interval=60,
    # execution_timeout=max(datetime.now().replace(hour=8, minute=20, second=0) - datetime.now(), timedelta(seconds=1)),
)

doris_jms_dwd__dwd_yl_oms_order_mark_expand_base_dt << jms_dwd__dwd_yl_oms_order_mark_expand_base_dt
